Custom Implementation for Mapping and Binding
Custom Implementation for Mapping and Binding
Sorm4j allows customizing how database query results are mapped to Java objects and how Java objects are bound to SQL parameters. This page explains how to implement and register custom conversions using ColumnValueToJavaObjectConverter and SqlParameterSetter.
Customizing Mapping Query Results to Java Objects
By default, Sorm4j provides automatic mapping from SQL column values to Java objects. However, you can register a custom converter using ColumnValueToJavaObjectConverter to handle specific data types.
Implementing a Custom ColumnValueToJavaObjectConverter
To define a custom conversion logic, implement the ColumnValueToJavaObjectConverter interface:
code: (java)
public class JacksonColumnValueToJavaObjectConverter implements ColumnValueToJavaObjectConverter {
private final ObjectMapper objectMapper;
private final OrmJsonContainers ormJsonContainers;
public JacksonColumnValueToJavaObjectConverter(ObjectMapper objectMapper, OrmJsonContainers ormJsonContainers) {
this.objectMapper = objectMapper;
this.ormJsonContainers = ormJsonContainers;
}
@Override
public boolean test(Class<?> toType) {
return ormJsonContainers.isOrmJsonContainer(toType);
}
@Override
public <T> T convertTo(ResultSet resultSet, int columnIndex, int columnType, Class<T> toType) throws SQLException {
try {
return objectMapper.readValue(resultSet.getBytes(columnIndex), toType);
} catch (IOException e) {
throw new SQLException("Failed to deserialize JSON column", e);
}
}
}
Registering the Custom Converter
After defining the converter, register it in SormContext:
code: (java)
SormContext context = SormContext.builder()
.addColumnValueToJavaObjectConverter(new JacksonColumnValueToJavaObjectConverter(objectMapper, ormJsonContainers))
.build();
Customizing Binding Java Objects to SQL Parameters
Sorm4j also allows customization of how Java objects are bound to SQL parameters via SqlParameterSetter.
Implementing a Custom SqlParameterSetter
To create a custom parameter setter, implement the SqlParameterSetter interface:
code: (java)
public class JacksonSqlParameterSetter implements SqlParameterSetter {
private final ObjectMapper objectMapper;
private final OrmJsonContainers ormJsonContainers;
public JacksonSqlParameterSetter(ObjectMapper objectMapper, OrmJsonContainers ormJsonContainers) {
this.objectMapper = objectMapper;
this.ormJsonContainers = ormJsonContainers;
}
@Override
public boolean test(PreparedStatement stmt, int parameterIndex, Object parameter) {
return parameter != null && ormJsonContainers.isOrmJsonContainer(parameter.getClass());
}
@Override
public void setParameter(PreparedStatement stmt, int parameterIndex, Object parameter) throws SQLException {
try {
stmt.setBytes(parameterIndex, objectMapper.writeValueAsBytes(parameter));
} catch (IOException e) {
throw new SQLException("Failed to serialize JSON parameter", e);
}
}
}
Registering the Custom Parameter Setter
Once the custom setter is implemented, register it in SormContext:
code: (java)
SormContext context = SormContext.builder()
.addSqlParameterSetter(new JacksonSqlParameterSetter(objectMapper, ormJsonContainers))
.build();
Summary
Use ColumnValueToJavaObjectConverter to define custom mappings from SQL column values to Java objects.
Use SqlParameterSetter to define custom bindings from Java objects to SQL parameters.
Register these implementations in SormContext to enable custom behavior.
With these custom implementations, Sorm4j can support complex data types, such as JSON, efficiently within database interactions.